package wibrst.skeleton {
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	/**
	 * ...
	 * @author wibrst@qq.com
	 */
	public class ContainMgr {
		
		static public const LAYER_DEBUG:String = "LAYER_DEBUG";
		static public const LAYER_TOP:String = "LAYER_TOP";
		static public const LAYER_DIALOGS:String = "LAYER_DIALOGS";
		static public const LAYER_CTRLS:String = "LAYER_CTRLS";
		static public const LAYER_MODULES:String = "LAYER_MODULES";
		static public const LAYER_BOTTOM:String = "LAYER_BOTTOM";
		
		static private var _vCont:DisplayObjectContainer;
		static private var _w:Number;
		static private var _h:Number;
		
		static public function getContLayer( idLayer:String ):Sprite {
			switch( idLayer ) {
			case LAYER_DEBUG: return _contLayerDebug;
			case LAYER_TOP:return _contLayerTop;
			case LAYER_DIALOGS:return _contLayerDialogs;
			case LAYER_CTRLS:return _contLayerCtrls;
			case LAYER_MODULES:return _contLayerModules;
			case LAYER_BOTTOM:return _contLayerBottom;
			} return null;
		}
		
		static public function initContainer( vCont:DisplayObjectContainer, w:Number, h:Number ):void {
			trace( "[ContainMgr] initContainer arguments:", vCont );
			if ( !_vCont ){
				_vCont = vCont;
				_w = w;
				_h = h;
				
				_contLayerDebug = new Sprite;
				_contLayerTop = new Sprite;
				_contLayerDialogs = new Sprite;
				_contLayerCtrls = new Sprite;
				_contLayerModules = new Sprite;
				_contLayerBottom = new Sprite;
				
				_contLayerDebug.mouseEnabled = false;
				_contLayerTop.mouseEnabled = false;
				_contLayerDialogs.mouseEnabled = false;
				_contLayerCtrls.mouseEnabled = false;
				_contLayerModules.mouseEnabled = false;
				_contLayerBottom.mouseEnabled = false;
				
				_contLayerDebug.name = LAYER_DEBUG;
				_contLayerTop.name = LAYER_TOP;
				_contLayerDialogs.name = LAYER_DIALOGS;
				_contLayerCtrls.name = LAYER_CTRLS;
				_contLayerModules.name = LAYER_MODULES;
				_contLayerBottom.name = LAYER_BOTTOM;
				
				_vCont.addChild( _contLayerBottom );
				_vCont.addChild( _contLayerModules );
				_vCont.addChild( _contLayerCtrls );
				_vCont.addChild( _contLayerDialogs );
				_vCont.addChild( _contLayerDebug );
				_vCont.addChild( _contLayerTop );
				
				/// special
				_vDialogIsolator = new Sprite;
				_vDialogIsolator.graphics.beginFill( 0x00ff00, 0 );
				_vDialogIsolator.graphics.drawRect( 0, 0, w, h );
				_vDialogIsolator.graphics.endFill();
				_vDialogIsolator.useHandCursor = false;
				_vDialogIsolator.addEventListener( MouseEvent.CLICK, isolaterHandler );
				
				_vDialogBk = new Shape;
				setBkStyle( 0x333333, .3 );
			}
		}
		
		static public function setBkVisible( bSet:Boolean ):void {
			_vDialogBk.visible = bSet;
		}
		
		static public function setBkStyle( nClr:Number, nAlpha:Number ):void {
				_vDialogBk.graphics.clear();
				_vDialogBk.graphics.beginFill( nClr, nAlpha );
				_vDialogBk.graphics.drawRect( 0, 0, _w, _h );
				_vDialogBk.graphics.endFill();
		}
		
		static private function isolaterHandler( evt:MouseEvent ):void {
			trace( " - - - - - - isolater - - - - - - " );
		}
		
		static public function addVisual( visual:DisplayObject, idLayer:String, bAutoSetVisible:Boolean = true, bDialogOverride:Boolean = true ):void {
			trace( "[ContainMgr] addVisual:", arguments );
			
			if( visual ){
				var contLayer:DisplayObjectContainer = getContLayer( idLayer );
				if ( ! contLayer ) {
					throw new Error("Invaid idLayer given!" );
				}
				
				if ( idLayer == LAYER_DIALOGS ) {
					if ( ! _nSumDialog ) {
						_contLayerDialogs.addChild( _vDialogBk );
						_contLayerDialogs.addChild( _vDialogIsolator );
					}
					++_nSumDialog;
					/// close up to the new visual
					if ( bDialogOverride )
						_contLayerDialogs.setChildIndex( _vDialogIsolator, _contLayerDialogs.numChildren - 1 ); 	
				}
				if ( bAutoSetVisible ) visual.visible = true;
				
				contLayer.addChild( visual );
			}
		}
		
		static public function removeVisual( visual:DisplayObject, bAutosetVisible:Boolean = true ):void {
			trace( "[ContainMgr] removeVisual:", arguments );
			
			if( visual ){
				if ( visual.parent ){
					if ( visual.parent == _contLayerDialogs ) {
						_contLayerDialogs.removeChild( visual );
						
						--_nSumDialog;
						if ( _nSumDialog ) {
							_contLayerDialogs.setChildIndex( _vDialogIsolator, _contLayerDialogs.numChildren - 2 );
						}else{
							_contLayerDialogs.removeChild( _vDialogIsolator );
							_contLayerDialogs.removeChild( _vDialogBk );
						}
					}else {
						visual.parent.removeChild( visual );
					}
				}
				if ( bAutosetVisible) visual.visible = false;
			}
		}
		
		/// container layers
		static private var _contLayerDebug:Sprite;
		static private var _contLayerTop:Sprite;
		static private var _contLayerDialogs:Sprite;
		static private var _contLayerCtrls:Sprite;
		static private var _contLayerModules:Sprite;
		static private var _contLayerBottom:Sprite;
		
		/// special visual
		static private var _vDialogBk:Shape;
		
		static private var _vDialogIsolator:Sprite;
		static private var _nSumDialog:Number = 0;
		
		static public function get w():Number {
			return _w;
		}
		
		static public function get h():Number {
			return _h;
		}
		
	}

}